'''
Descripttion: 
Author: yubo
Date: 2022-08-05 09:59:25
LastEditTime: 2022-10-31 13:48:53
'''
# coding=utf-8
import sys
import xlrd


def get_row_range(sheet, item_id):
    row_start = 0
    row_end = 0
    for line in range(sheet.nrows):
        line_1 = sheet.row_values(line)[1]
        if line_1 == str(item_id):
            if row_start == 0:
                row_start = int(line)
        elif line_1 != str(item_id) and row_start != 0 :
            row_end = line
            break

    if row_start == 0:
        print("check the accuracy of the file contents!")
        sys.exit()    

    if row_end == 0:
        row_end = sheet.nrows;
    return row_start, row_end

def read_dict(item_id):
    workbook = xlrd.open_workbook("./UFX-cqs-ext-20221010.xlsx")

    sheet = workbook.sheet_by_name("数据字典")

    row_start, row_end = get_row_range(sheet, item_id)

    tmpfile = open(file='dict_tmp.txt', mode='w', encoding='utf-8')

    for line in range(row_start, row_end) :
        id = sheet.row_values(line)[3]
        value = sheet.row_values(line)[4]

        tmpfile.writelines("\"{}\" : \"{}\",\n".format(id, value))

    tmpfile.close()

if __name__ == '__main__':
    if len(sys.argv) != 2:
       print("wrong usage")
       sys.exit()
    
    item_id = sys.argv[1]
    read_dict(item_id)